//
//  BubbleViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/7/24.
//

import Foundation
import LeanCloud
class MyBubbleCommentViewModel: BaseViewModel {
    @Published var items = [BubbleMallDataBean]()
    @Published var selectedIndex: Int = 0
    @Published var status = ""
    
    
    func getData(_ isRefresh: Bool = false) {
        requestList(isRefresh: isRefresh) {
            try check(userManager.isLogin(), "请先登录")
            let query = LCQuery(className: "BubbleMallData")
            query.whereKey("hide", .notEqualTo(true))
            query.whereKey("userId", .equalTo(userManager.getUserId()))
            let list =  try await LeancloudManager.instance.getList(query: query, modeType: BubbleMallDataBean.self)
            try check(!isRefresh || !list.isEmpty, "暂无数据")
            return list
        } suc: { diarys in
            self.status = ""
            self.items.append(contentsOf: diarys)
        } fail: {
            self.status = $0.message
        }
    }
    
    func doBubble(bean: DiaryBean, id: String) {
        request {
            bean.bubbleId = id
            try await BackupsManager.shared.getDelegateDiary()?.saveDiary(bean: bean)
            return ""
        } suc: { _ in
            showToast("装扮成功")
            NotificationCenter.default.post(name: .updateBubbleComment, object: bean)
        } fail: {
            showToast($0.message)
        }
    }
    
}
